<?php

namespace Kicker\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\File\UploadedFile;

// these import the entity team
use Kicker\MainBundle\Entity\Team;
use Kicker\MainBundle\Entity\Document;

/**
 * Description of TeamController
 */
class TeamController extends Controller {
    /**
     * @Route("/", name="_team")
     */
    public function indexAction() {
        
        return $this->redirect($this->generateUrl('_team_display'));
    }
    
    /**
     * @Route("/display", name="_team_display")
     * @Template()
     */
    public function displayAction() {
        
        $team = $this->getDoctrine()->getRepository('KickerMainBundle:Team');
        $result = $team->findAll();
        
        return $this->render('KickerMainBundle:Team:index.html.twig', array(
            'target' => 'team',
            'menu' => 'display',
            'teams' => $result
        ));
    }
    
    
    /**
     * 
     * @Route("user/edit/{id}", name="_team_edit")
     * @Route("user/add", name="_team_add")
     * @Template("KickerMainBundle:Team:index.html.twig") 
     */
    public function addEditAction($id = null) {
        
        $warning = array();
        $team = $id ? $team = $this->getDoctrine()->getRepository('KickerMainBundle:Team')->find($id) : new Team();
        if ( !empty($_POST) ) {    
            // Get request
//            echo "<pre>"; print_r($this->get('request')->files->all()); echo "</pre>";
//            echo "<pre>"; print_r($this->get('request')->files->get("logo")); echo "</pre>";
//            die();
            $request = $this->get('request')->request;
            
            $player1 = $request->get('player1');
            $player2 = $request->get('player2');
            $email1 = $request->get('email1');
            $email2 = $request->get('email2');
            $name = $request->get('name');
            
            // MANAGER UPLOADING OF PICTURE //
            $file = $this->get('request')->files->get("logo");
            if(!empty($file)){
                $logo = new Document();
                $logo->setFile($file);
                $logo->setPath("uploaded");
                $team->setLogo($logo);
            }
            
//            $file = $_FILES['logo'];
//            if(!empty($file)){
//    //            $dir = '@KickerMainBundle/resources/uploaded';
//                $dir = __DIR__ ."/uploaded";
//                $dir_save = $dir ."/" .rand(1, 99999). "_" .$file["name"];
//                var_dump($dir_save);
//                move_uploaded_file($file["tmp_name"], $dir_save);
//    //            $dir = "uploaded";
//    //            $fileName = $request->getFileName('logo');
//    //            $this->getRequest()->moveFile('file', $dir.'/'.$fileName);
//                /////////////////////////////////
//                $team->setLogo($dir_save);
//            }
            
            empty($player1) ? array_push($warning, 'player1') : $team->setPlayer1($player1);
            empty($player2) ? array_push($warning, 'player2') : $team->setPlayer2($player2);
            empty($email1) ? array_push($warning, 'email1') : $team->setEmail1($email1);
            empty($email2) ? array_push($warning, 'email2') : $team->setEmail2($email2);
            empty($name) ? array_push($warning, 'name') : $team->setName($name);
            
            if(count($warning) == 0){
                $db = $this->getDoctrine()->getManager();
                $db->persist($team);
                $db->flush();
                $db->clear();
                return $this->redirect($this->generateUrl('_team_display'));
            } else{
                return $this->render('KickerMainBundle:Team:index.html.twig', array(
                    'target' => 'team',
                    'menu' => 'add',
                    'warning' => true
                ));
            }
        } else{
            // New Team
            if( $id == null){
                return array(
                    'target' => 'team',
                    'menu' => 'add',
                    'warning' => false
                );
            } else{
                return array(
                    'target' => 'team',
                    'menu' => 'edit',
                    'team' => $team,
                    'warning' => false
                );
            }
        }
    }
    
    /**
     * @Route("/remove/{id}", name="_team_remove")
     */
    public function removeAction( $id=null ) {

        $team = $this->getDoctrine()->getRepository('KickerMainBundle:Team')->find($id);
        
        $em = $this->getDoctrine()->getEntityManager();
        $em->remove($team);
        $em->flush();

        // Redirect to display teams
        return $this->redirect($this->generateUrl('_team_display'));
        
    }
}

?>
